//
//  DatePickerView.m
//  Daybook
//
//  Created by dllo on 16/5/19.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "DatePickerView.h"
#import "Common.h"

@interface DatePickerView ()

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UIView *upView;
@property (nonatomic, strong) UILabel *upLabel;
@property (nonatomic, strong) UIView *upOneLineView;
@property (nonatomic, strong) UIButton *beginDateButton;
@property (nonatomic, strong) UILabel *toLabel;
@property (nonatomic, strong) UIButton *endDateButton;
@property (nonatomic, strong) UIView *upSecondLineView;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *sureButton;

@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong)  UIDatePicker *datePicker;
@property (nonatomic, strong) NSDateFormatter *formatter;

@property (nonatomic, assign) BOOL isClick;
@property (nonatomic, strong) NSString *timeStr;

@end
@implementation DatePickerView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 初始化设置
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        self.frame = CGRectMake(0, 0, MAIN_WIDTH, MAIN_HEIGHT);
        [self createTopView];
        [self createBottomView];
    }
    return self;
}

- (void)setIsGetDate:(BOOL)isGetDate {
    _isGetDate = isGetDate;
}

- (void)setIsRightDetailDate:(BOOL)isRightDetailDate {
    _isRightDetailDate = isRightDetailDate;
}

- (void)createTopView {
    self.isClick = NO;
    self.upView = [[UIView alloc] initWithFrame:CGRectMake(10 / 414.0 *MAIN_WIDTH, 190 * MAIN_HEIGHT / 736, MAIN_WIDTH - 20 / 414.0 *MAIN_WIDTH, MAIN_HEIGHT * (191 / 736.0))];
    [self addSubview:_upView];
    _upView.backgroundColor = [UIColor whiteColor];
    self.upView.layer.cornerRadius = MAIN_HEIGHT * (5 / 736.0);
    self.upView.layer.masksToBounds = YES;
    
    self.upLabel  = [[UILabel alloc] initWithFrame:CGRectMake(10 / 414.0 *MAIN_WIDTH, 10 * MAIN_HEIGHT / 736, MAIN_WIDTH - 20 / 414.0 *MAIN_WIDTH, 20 * MAIN_HEIGHT / 736)];
    [self.upView addSubview:_upLabel];
    self.upLabel.center = CGPointMake(MAIN_WIDTH / 2, 20 * MAIN_HEIGHT / 736);
    _upLabel.text = @"请选择开始时间";
    _upLabel.textAlignment = 1;
    _upLabel.font = [UIFont systemFontOfSize:16];
    
    self.upOneLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 41, MAIN_WIDTH - 20 / 414.0 *MAIN_WIDTH, 1)];
    [self.upView addSubview:_upOneLineView];
    _upOneLineView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.2];
    
    self.beginDateButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(0, 0, 100 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH);
        tempButton.center = CGPointMake(MAIN_WIDTH / 2 - 80 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (90 / 736.0)));
        [self.upView addSubview:tempButton];
        [tempButton setTitle:[GetTimeNow getDateNow] forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(beginDateButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        //        tempButton.backgroundColor = [UIColor lightGrayColor];
        //        tempButton.layer.borderWidth = 1;
        //        tempButton.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        [tempButton setTitleColor:BGCOLOR forState:UIControlStateNormal];
        tempButton.titleLabel.font = [UIFont systemFontOfSize:16];
        tempButton;
    });
    
    self.endDateButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(0, 0, 100 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH);
        tempButton.center = CGPointMake(MAIN_WIDTH / 2  + 80 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (90 / 736.0)));
        [self.upView addSubview:tempButton];
        [tempButton setTitle:[GetTimeNow getDateNow] forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(endDateButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [tempButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        tempButton.titleLabel.font = [UIFont systemFontOfSize:16];
        //        tempButton.backgroundColor = [UIColor lightGrayColor];
        //        tempButton.layer.borderWidth = 1;
        //        tempButton.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        tempButton;
    });
    
    self.toLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH)];
        tempLabel.center = CGPointMake(MAIN_WIDTH / 2, (MAIN_HEIGHT * (90 / 736.0)));
        [self.upView addSubview:tempLabel];
        tempLabel.textAlignment = 1;
        [tempLabel setText:@"至"];
        tempLabel;
    });
    
    self.upSecondLineView = [[UIView alloc] initWithFrame:CGRectMake(0, MAIN_HEIGHT * (140 / 736.0), MAIN_WIDTH - 20 / 414.0 * MAIN_WIDTH, 1)];
    [self.upView addSubview:_upSecondLineView];
    self.upSecondLineView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.2];
    
    self.cancelButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(10 / 414.0 *MAIN_WIDTH, MAIN_HEIGHT * (141 / 736.0), MAIN_WIDTH / 2 - 10 / 414.0 *MAIN_WIDTH, 50 / 414.0 * MAIN_WIDTH);
        [self.upView addSubview:tempButton];
        [tempButton setTitle:@"取消" forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        //        tempButton.backgroundColor = [UIColor lightGrayColor];
        //        tempButton.layer.borderWidth = 1;
        //        tempButton.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        [tempButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        tempButton.titleLabel.font = [UIFont systemFontOfSize:16];
        tempButton;
    });
    
    self.sureButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(MAIN_WIDTH / 2 - 10 / 414.0 *MAIN_WIDTH, MAIN_HEIGHT * (141 / 736.0), MAIN_WIDTH / 2 - 10 / 414.0 * MAIN_WIDTH, 50 / 414.0 * MAIN_WIDTH);
        [self.upView addSubview:tempButton];
        [tempButton setTitle:@"确认" forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(sureButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        //        tempButton.backgroundColor = [UIColor lightGrayColor];
        //        tempButton.layer.borderWidth = 1;
        //        tempButton.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        [tempButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        tempButton.titleLabel.font = [UIFont systemFontOfSize:16];
        tempButton;
    });
}

- (void)createBottomView {
    // 设置时间格式转换器
    self.formatter = [[NSDateFormatter alloc] init];
    self.formatter.dateFormat = @"yyyy-MM-dd";
    
    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(10 / 414.0 *MAIN_WIDTH, MAIN_HEIGHT * (450 / 736.0), MAIN_WIDTH - 20 / 414.0 *MAIN_WIDTH, MAIN_HEIGHT * (200 / 736.0))];
    [self addSubview:_bottomView];
    self.bottomView.backgroundColor = [UIColor whiteColor];
    self.bottomView.layer.cornerRadius = MAIN_HEIGHT * (5 / 736.0);
    self.bottomView.layer.masksToBounds = YES;
    
    // 配置DatePicker
    self.datePicker = [[UIDatePicker alloc] initWithFrame:self.bottomView.bounds];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    [self.bottomView addSubview:_datePicker];
    self.datePicker.maximumDate = [NSDate date];
    CGRect newFrame = self.datePicker.frame;
    newFrame.size.width = MAIN_WIDTH - 20 / 414.0 *MAIN_WIDTH;
    self.datePicker.frame = newFrame;
    [self.datePicker addTarget:self action:@selector(datePickerValueChange:) forControlEvents:UIControlEventValueChanged];
}

- (void)beginDateButtonAction:(UIButton *)button {
    self.isClick = NO;
    self.upLabel.text = @"请选择开始时间";
    [button setTitleColor:BGCOLOR forState:UIControlStateNormal];
    [self.endDateButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}

- (void)endDateButtonAction:(UIButton *)button {
    self.isClick = YES;
    self.upLabel.text = @"请选择结束时间";
    [button setTitleColor:BGCOLOR forState:UIControlStateNormal];
    [self.beginDateButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}

- (void)datePickerValueChange:(UIDatePicker *)datePicker {
    self.timeStr = [self.formatter stringFromDate:self.datePicker.date];
    if (self.isClick) {
        NSString *beginDateStr = [self.beginDateButton.titleLabel.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
        NSString *endDateStr = [self.timeStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
        
        if (beginDateStr.integerValue > endDateStr.integerValue) {
            self.upLabel.text = @"开始时间须小于结束时间";
            [self.endDateButton setTitle:self.beginDateButton.titleLabel.text forState:UIControlStateNormal];
        } else {
            self.upLabel.text = @"请选择结束时间";
            [self.endDateButton setTitle:self.timeStr forState:UIControlStateNormal];
        }
    }else {
        self.upLabel.text = @"请选择开始时间";
        [self.beginDateButton setTitle:self.timeStr forState:UIControlStateNormal];
    }
}

- (void)show {
    [UIView animateWithDuration:1 animations:^{
        self.alpha = 1;
    }];
}

- (void)hide {
    self.alpha = 0;
}


- (void)cancelButtonAction:(UIButton *)button {
    [self.delegate cancelButtonAction];
}

- (void)sureButtonAction:(UIButton *)button {
    if (_isGetDate) {
        [self.delegate sureButtonActionWithBeginTime:self.beginDateButton.titleLabel.text endTime:self.endDateButton.titleLabel.text];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeTime" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:self.beginDateButton.titleLabel.text, @"beginTime", self.endDateButton.titleLabel.text,@"endTime",nil]];
    }
    if (_isRightDetailDate) {
        [self.delegate rightDetailActionWithBeginTime:self.beginDateButton.titleLabel.text endTime:self.endDateButton.titleLabel.text];
    }
}



@end
